/**
 * Created by Administrator on 2017/11/28.
 */
myApp.controller('listCtrl',function ($scope,$rootScope,$sce,$uibModal,$log,$filter,$http,$timeout,$stateParams,$state,httpService,commonUtil) {
	

	$rootScope.$on('showInfo',function (event,d) {
		for(var i = 0; i < $rootScope.pageData.length; i++){
			if($rootScope.pageData[i].id == d.id){
                var formBoxData={
                    title: "查看资产",
                    modalApi: "assets/updateAssetsManagement",
                    confirmFunc: "moreData()",
                    checkedAsset: $rootScope.pageData[i]
                };
                $uibModal.open({
                    templateUrl: "modal/asset_detail/asset_detail.html",
                    controller: "asset_detail",
                    size: "50",
                    resolve: {
                        formBoxData: function () {
                            return formBoxData;
                        }
                    }
                });
				break;
			}
		}
	});
    $rootScope.$on('to-list', function(event, data) {
        if(data == "moreData"){
            $scope.moreData();
		}
    });

	$scope.addAssets = function () {
		var formBoxData={
				title:'新增资产'
		}
		var modalInstance = $uibModal.open({
			templateUrl: 'modal/asset_add/asset_detail.html',
			controller: 'asset_add',
			backdrop: "static",
			size: 'lg',
			resolve: {
				formBoxData: function () {
					return formBoxData;
				}
			}
		});
		modalInstance.result.then(function (formData) {
			console.log(formData);
			$scope.saveFormData(formData);
		}, function () {
			$log.info('Modal dismissed at: ' + new Date());
		});
	};
	
	$scope.editAssets = function () {
		var i = 0;
		var editItem;
		angular.forEach($scope.showData, function(item01, index01) {
			if (item01['checked'] == true) {
				editItem=angular.copy(item01);
				delete editItem.checked;
				i++
			}
		})
		if (i != 1) {
			return;
		}
		var formBoxData={
				title:'编辑资产',
				formData:editItem
		};
		var modalInstance = $uibModal.open({
			templateUrl: 'html/systems/assets/delete.html',
			controller: 'asset_update',
			backdrop: "static",
			size: 'lg',
			resolve: {
				formBoxData: function () {
					return  formBoxData;
				}
			}
		});
		
		
		modalInstance.result.then(function (formData) {
			console.log(formData);
			$scope.updateFormData(formData);
		}, function () {
			$log.info('Modal dismissed at: ' + new Date());
		});
		
		
	};
	
	
	$scope.operationAssets=function(title,useStatus,dealStatus,useStatusFlag){
		var i = 0;
		var checkedItems=$scope.showData.filter(function(item,index){
			return item.checked;
		});
		
		if (checkedItems.length <= 0) {
			return;
		}
		
		var formBoxData={
				title:title,
				useStatus:useStatus,
				dealStatus:dealStatus,
				checkNum:checkedItems.length,
				useStatusFlag:useStatusFlag
		};
		var modalInstance = $uibModal.open({
			templateUrl: 'html/systems/assets/receiveAssets.html',
			controller: 'receiveAssetsCtrl',
			backdrop: "static",
//			size: 'sm',
			resolve: {
				formBoxData: function () {
					return formBoxData;
				}
			}
		});
		
		modalInstance.result.then(function (formData) {
			console.log(formData);
			
			var checkedIds = checkedItems.map(function(item,index){
				return item.id;
			});
			console.log(checkedIds,formData);
			$scope.changeUseStatus(checkedIds,formData);
			
			
		}, function () {
			$log.info('Modal dismissed at: ' + new Date());
		});
	}
	
	
	$scope.receiveAssets = function () {
		var title="领用资产";
		var useStatus='领用';
		var dealStatus='领用';
		var useStatusFlag='receive';
		$scope.operationAssets(title,useStatus,dealStatus,useStatusFlag);
	};
	
	$scope.borrowAssets = function () {
		var title="借用资产";
		var useStatus='借用';
		var dealStatus='借用';
		var useStatusFlag='borrow';
		$scope.operationAssets(title,useStatus,dealStatus,useStatusFlag);
	};
	
	$scope.backAssets = function () {
		var title="退回资产";
		var useStatus='空闲';
		var dealStatus='退回';
		var useStatusFlag='back';
		$scope.operationAssets(title,useStatus,dealStatus,useStatusFlag);
	};
    
    
    /*----------------------------------*/
    
    
    //操作按钮
    $scope.toolsItem=[
		{
			'text':'增加',
			'func' :$scope.addAssets
		},
		{
    		'text' : '修改',
    		'func' : $scope.editAssets
//		'func' : function() {
//			console.log('修改');
//			var i = 0;
//			var editItem;
//			angular.forEach($scope.showData, function(item01, index01) {
//				if (item01['checked'] == true) {
//					editItem=angular.copy(item01);
//					delete editItem.checked;
//					i++
//				}
//			})
//			if (i == 1) {
//				console.log('broadcast');
//				$scope.formData=editItem;
//				$scope.moreOperationData();
//				$rootScope.$broadcast('changeEditFormBox', {
//					title : '修改等级模板',
//					func : $scope.updateFormData
//				});
//			}
//			
//			// 新增
//		}
    	},
		{
			'text':'删除',
			'url':'assets/deleteAssetsManagement'
		},
		{
			'text':'领用',
			'func' : $scope.receiveAssets
		},
		{
			'text':'借用',
			'func' : $scope.borrowAssets
		},
		{
			'text':'退回',
			'func' : $scope.backAssets
		}
		
	];
    
    
    $scope.saveFormData=function(formData){
		console.log('saveItem');
		console.log(formData);
		httpService.postRequest('assets/insertAssetsManagement',formData).then(function(res){
			var result=res.data;
			if(result.resultCode==0){
				var assetsOperation={
					assetsId:result.data.id,
					dealStatus:'入库'
				}
				$scope.moreData();
				httpService.insertAssetsOperation(assetsOperation);
			}
			console.log(res);
		});
	}
	
	
    $scope.updateFormData=function(formData){
    	console.log('updateItem');
    	console.log(formData);
    	httpService.postRequest('assets/updateAssetsManagement',formData).then(function(res){
    		var result=res.data;
    		if(result.resultCode==0){
    			$scope.moreData();
    		}
    		console.log(res);
    	});
    }
    
    $scope.pageConfig={
    		"currentPage":1,
			"showCount":5,
        };
    $scope.$on('changeSearch',function(event,data){
    	var pd={};
    	pd.keyword=data.keyword;
    	$scope.pageData.pd=pd;
    	$scope.moreData();
    });
    
    
});



myApp.controller('receiveAssetsCtrl', function ($scope, $uibModalInstance,formBoxData,httpService) {
	
	/*关闭的图片*/
	$scope.closeImg={'img':'images/close-default.png','defaultImg':'images/close-default.png','downImg':'images/close-btndown.png','hoverImg':'images/close-hover.png'};
	$scope.title=formBoxData.title;
	$scope.checkNum=formBoxData.checkNum;
	$scope.useStatusFlag=formBoxData.useStatusFlag;
	$scope.formData={
			userInfo:{
					userId:'kjal535sjdfl43ssfs123123df',
					name:'张三',
					email:'4356565@qq.com',
					phone:'1856563232'
			},
			operationData:{
				useStatus:formBoxData.useStatus,
				dealStatus:formBoxData.dealStatus
			}
	}
	
	
	$scope.ok = function () {
		$uibModalInstance.close($scope.formData);
	};
	
	$scope.cancel = function () {
		$uibModalInstance.dismiss('cancel');
	};
});

